Module Module1

    Public DbCon As New OleDb.OleDbConnection
    Public DbCom As New OleDb.OleDbCommand
    Public Read As OleDb.OleDbDataReader
    Public T_id, U_id
    Public SQL, namafrm As String

    Public Sub open_con()
        If DbCon.State = 1 Then
            MsgBox("Sedang Terkoneksi", MsgBoxStyle.Information, "Connection Error")
        Else
            DbCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Herm@n\My Documents\Findy\Unit Maintenance\Copy Of Unit Maintenance.mdb;User Id=admin;Password=;"
            DbCon.Open()
        End If
    End Sub

    Public Sub open_db(ByVal query As String)
        If DbCon.State = 0 Then
            open_con()
        End If
        DbCom.Connection = DbCon
        DbCom.CommandType = CommandType.Text
        DbCom.CommandText = query
        Read = DbCom.ExecuteReader
    End Sub
    Public Sub exec_qr(ByVal query As String)
        If DbCon.State = 0 Then
            open_con()
        End If
        DbCom.Connection = DbCon
        DbCom.CommandType = CommandType.Text
        DbCom.CommandText = query
        Read = DbCom.ExecuteReader
        Read.Close()
        DbCon.Close()
    End Sub
    Public Sub close_db()
        Read.Close()
        DbCon.Close()
    End Sub
    Public Function isnull(ByVal cek)
        If cek Is DBNull.Value Then
            isnull = ""
        Else
            isnull = cek
        End If
    End Function
    Public Function add_query(ByVal query, ByVal table, ByVal value, ByVal type) As String
        'Dim txt As String()
        Dim q As String
        Dim cut As Integer
        Dim x As String = ""
        Dim y As String = ""
        If query <> "" Then
            cut = Strings.InStr(query, ")")
            x = Strings.Left(query, cut - 1)
            y = Strings.Mid(query, cut + 1)
        End If
        'txt = query.Split(")")
        Select Case type
            Case "s" 'string
                q = "'"
            Case "t" 'time
                q = "'"
            Case "d" 'date
                q = "'"
            Case Else
                q = "" 'n = numerik
        End Select
        If type = "s" Then
            If value = "" Then
                Return query
            End If
        End If
        If type = "b" Then 'boolean/checkbox
            If value = "0" Then
                Return query
            End If
        End If
        If query = "" Then
            Return table & ") values (" & q & isnull(value) & q
        Else
            'txt(0) = txt(0) & "," & table
            'txt(1) = txt(1) & "," & q & isnull(value) & q
            'Return txt(0) & ")" & txt(1)
            x = x & "," & table
            y = y & "," & q & isnull(value) & q
            Return x & ")" & y
        End If
    End Function
    Public Function edit_query(ByVal query, ByVal table, ByVal param, ByVal value, ByVal type) As String
        Dim q As String
        Select Case type
            Case "s"
                q = "'"
            Case "t"
                q = "'"
            Case "d"
                q = "'"
            Case Else
                q = ""
        End Select
        If type = "s" Then
            If value = param Then
                Return query
            End If
        End If
        If query = "" Then
            Return table & "=" & q & isnull(value) & q
        Else
            Return query & "," & table & "=" & q & isnull(value) & q
        End If
    End Function
End Module

